package com.programmingteam.wallpaperslideshow.ui.fragments;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.programmingteam.wallpaperslideshow.R;
import com.programmingteam.wallpaperslideshow.ui.GalleryActivity;
import com.programmingteam.wallpaperslideshow.ui.ImageDetailsActivity;

/**
 * Main dependencies:
 * com.programmingteam.wallpaperslideshow.ui.GalleryActivity
 * com.programmingteam.wallpaperslideshow.ui.fragments.GalleryFragment
 * android.support.v4.view.ViewPager
 * android.support.v4.view.PagerAdapter
 * @author Jakub Skierbiszewski
 */
public class GalleryFragment extends Fragment implements OnClickListener {
	public final static String TAG = "WallpaperSlideshow";
	public final static int VIEW_INITIAL = 0;
	public final static int VIEW_NEXT = 1;
	public final static int IMAGES_COUNT_INITIAL = 3;
	public final static int IMAGES_COUNT_NEXT = 6;
	public final static int MSG_REFRESH_IMAGES = 0;
	private final static Bitmap mBitmapBackground = Bitmap.createBitmap(new int[] { Color.BLACK }, 1, 1, Config.ARGB_8888);
	private final static int[] IMAGE_VIEW_LIST_IDS = {
			R.id.gallery_image1, R.id.gallery_image2, R.id.gallery_image3,
			R.id.gallery_image4, R.id.gallery_image5, R.id.gallery_image6 };
	private int mViewType = VIEW_INITIAL;
	private int mViewStartPos = 0;
	private View mRootView;

	public GalleryFragment(int position) {
		super();
		mViewStartPos = position;
		mViewType = position == 0 ? VIEW_INITIAL : VIEW_NEXT;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//Always inflate rootView!
		mRootView = inflateRootView(inflater);
		return mRootView;
	}

	public void onStart() {
		GalleryActivity galleryActivity;
		galleryActivity = (GalleryActivity) getActivity();
		galleryActivity.loadImagePathsAsync(this, mViewStartPos, getImageCount());
		super.onStart();
	}

	public void onStop() {
		ImageView iv;

		for (int i = 0; i < IMAGE_VIEW_LIST_IDS.length; i++) {
			iv = (ImageView) mRootView.findViewById(IMAGE_VIEW_LIST_IDS[i]);
			if (iv != null) {
				iv.setImageBitmap(mBitmapBackground);
			}
		}
		System.gc();
		super.onStop();
	}

	private View inflateRootView(LayoutInflater inflater) {
		View v;
		ImageView iv;
		//Inflate root view
		switch (mViewType) {
		case VIEW_INITIAL:
			v = inflater.inflate(R.layout.layout_imagegallery_main, null);
			break;
		case VIEW_NEXT:
			v = inflater.inflate(R.layout.layout_imagegallery_next, null);
			break;
		default:
			v = inflater.inflate(R.layout.layout_imagegallery_next, null);
		}
		//setup views
		for (int i = 0; i < IMAGE_VIEW_LIST_IDS.length; i++) {
			iv = (ImageView) v.findViewById(IMAGE_VIEW_LIST_IDS[i]);
			if (iv != null) {
				iv.setScaleType(ScaleType.CENTER_INSIDE);
				iv.setImageResource(R.drawable.loading);
			}
		}
		return v;
	}

	/** Image Loader (set bitmap on proper view) */
	private Handler mHandlerRefreshImages = new Handler() {
		/** MSG:
		 * 	msg.arg1 = position
		 *  msg.obj  = image bitmap
		 */
		@Override
		public void handleMessage(Message msg) {
			ImageView iv;
			int imageViewNum;
			Bitmap imageBitmap;
			Drawable imageDrawable;

			imageViewNum = msg.arg1 - mViewStartPos;
			iv = (ImageView) mRootView.findViewById(IMAGE_VIEW_LIST_IDS[imageViewNum]);
			if (iv != null) {
				if (msg.obj != null && msg.obj instanceof Bitmap) { //Set image
					imageBitmap = (Bitmap) msg.obj;
					iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
					iv.setImageBitmap(imageBitmap);
					iv.setOnClickListener(GalleryFragment.this);
				} else if (msg.obj != null && msg.obj instanceof Drawable) {
					imageDrawable = (Drawable) msg.obj;
					iv.setImageDrawable(imageDrawable);
					iv.setOnClickListener(GalleryFragment.this);
				}
				else { // No image for that position, just set null
					iv.setImageBitmap(mBitmapBackground);
				}
			}
		}
	};

	/**
	 * @param position position of image
	 * @param image pair (imageId, imageBitmap) or null if image is unavailable.
	 */
	public void setImage(int position, Bitmap imageBitmap) {
		Message msg;
		msg = mHandlerRefreshImages.obtainMessage();
		msg.arg1 = position;
		msg.obj = imageBitmap;
		mHandlerRefreshImages.sendMessage(msg);
	}

	public int getImageCount() {
		return mViewType == VIEW_INITIAL ? IMAGES_COUNT_INITIAL : IMAGES_COUNT_NEXT;
	}

	@Override
	public void onClick(View v) {
		getActivity().startActivity(new Intent(getActivity(), ImageDetailsActivity.class));
	}
}
